package test27.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherMethod {
    public static void main(String[] args) {
        String content = "hello edu jack hspedutom hello smith hello hspedu hspedu";
        String regStr = "hello";

        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);

        while (matcher.find()){
            System.out.println("=================");
            System.out.println(matcher.start());//拿到匹配到的开始的索引
            System.out.println(matcher.end());//拿到匹配到的介素索引+1
            System.out.println("找到: "+content.substring(matcher.start(),matcher.end()));//substring包左不包右
        }

        //整体匹配方法，常用于，去校验某个字符串是否满足某个规则
        System.out.println("整体匹配=" + matcher.matches());

        //完成如果content 有 hspedu 替换成  hahaha
        regStr = "hspedu";
        pattern = Pattern.compile(regStr);
        matcher = pattern.matcher(content);
        //注意：返回的字符串才是替换后的字符串 原来的 content 不变化
        //如果想改变原来的，直接赋给原来的content就行了
        String newContent = matcher.replaceAll("hahaha");
        System.out.println("newContent=" + newContent);
        System.out.println("content=" + content);
    }
}
